package com.bingo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bingo.dao.UserMapper;
import com.bingo.entity.User;
import com.bingo.entity.dto.req.CheckUser;
import com.bingo.service.UserService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author Bingo
 * @version 1.0
 * @description 用户服务实现类
 * @date 2024/3/26 17:26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    //根据用户编号获取用户
    @Override
    public User getUserByNo(String userNo) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userNo);
        User user = this.getOne(wrapper);
        return user;
    }

    //检验账号密码是否正确
    @Override
    public boolean checkPassword(String userNo, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userNo);
        User user = this.getOne(wrapper);
        //不存在该用户
        if (user==null){
            return false;
        }else {
            if (user.getPassword().equals(password)){
                return true;
            }else {
                return false;
            }
        }
    }

    //获取所有用户信息
    @Override
    public HashMap<String,String> getAllUser() {
        //查询全部用户
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        List<User> list = this.list(wrapper);

        //如果list为空，返回null
        if (list==null || list.size()==0){
            return null;
        }

        //只保留用户编号和姓名
        HashMap<String, String> map = new HashMap<>();
        for (User user : list) {
            map.put(user.getUserId(),user.getName());
        }

        return map;

    }

    //注册用户
    @Override
    public boolean register(User user) {
        User userByNo = this.getUserByNo(user.getUserId());

        //已经存在该用户
        if (userByNo!=null){
            return false;
        }

        //默认密码为123456
        user.setPassword("e10adc3949ba59abbe56e057f20f883e");


        //设置权限
        if (user.getType()==0){
            user.setAuthority("超级管理员");
        }else if (user.getType()==1){
            user.setAuthority("管理员");
        }else if (user.getType()==2){
            user.setAuthority("场长");
        }else {
            //不存在该类型用户
            return false;
        }

        return this.save(user);
    }

    @Override
    public boolean updateUser(User user) {

        User userByNo = this.getUserByNo(user.getUserId());

        if (userByNo !=null){
            userByNo.setName(user.getName());
            userByNo.setPhone(user.getPhone());
            userByNo.setType(user.getType());
            if (userByNo.getType()==0){
                userByNo.setAuthority("超级管理员");
            }
            if (userByNo.getType()==1){
                userByNo.setAuthority("管理员");
            }
            if (userByNo.getType()==2){
                userByNo.setAuthority("场长");
            }
            return this.updateById(userByNo);
        }
        return false;
    }

    @Override
    public boolean deleteUser(String userNo) {
        User userByNo = this.getUserByNo(userNo);
        if (userByNo!=null){
            return this.removeById(userByNo);
        }
        return false;
    }


    //修改密码
    @Override
    public boolean updatePassword(CheckUser user) {
        if (user==null || user.getUserNo()==null || user.getPassword()==null || user.getOldPassword()==null){
            return false;
        }
        User userByNo = this.getUserByNo(user.getUserNo());
        if (userByNo!=null){
            if (userByNo.getPassword().equals(user.getOldPassword())){
                userByNo.setPassword(user.getPassword());
                return this.updateById(userByNo);
            }
        }
        return false;
    }


}
